#!/usr/bin/python
# -*- coding: utf-8 -*-

import glob
import re
import sys
import string
import codecs

class lines:
    ''' Read big text file as iterator. '''
    def __init__(self , flpath_s):
        self.flpath_s = flpath_s
        self.sfile_fl = open(flpath_s, 'rb')

    def __getitem__( self , index ):
        line_s = self.sfile_fl.readline()
        if line_s:
            return line_s
        else:
            raise IndexError

n = '\x0D\x0A'

def main(inFile='', skipHeader=False, cd='\t'):
    ''' '''

    o = open(inFile + '.import.sql', 'wb')
    o.write('BEGIN' + n)
    d = open(inFile + '.dropped.import.dat', 'wb')

    for f in glob.glob(inFile):

        fname = string.split(f, '.')[0]
        #pathfname = f[f.find('...'):] # cutting path at some know point
        print fname

        ref_cd_cnt = 0
        lino = 0

        for line in lines(f):
            lino += 1

            line = re.sub(n, '', line)
            line = re.sub('\x0A', '', line)
            line = re.sub('\x0D', '', line)

            cols = string.split(line, cd)

            if ref_cd_cnt == 0:
                ref_cd_cnt = len(cols)

            if lino == 1 and skipHeader == True:
                d.write(line + n)
            else:
                line_len = len(cols)
                if line_len == ref_cd_cnt:
                    r = ''
                    for c in cols:
                        r += """'%s', """ % (c,)
                    
                    o.write("INSERT INTO %s VALUES (%s);" % (fname, r[:-2]) + n)
                else:
                    d.write(line + n)

    o.write('END;' + n)
    o.close()
    d.close()

if __name__ == "__main__":
    ''' '''
    if  len(sys.argv) == 2:
        main(sys.argv[1])
    if  len(sys.argv) == 3 and sys.argv[2] in ('header=1', 'header', 'hasheader'):
        main(inFile=sys.argv[1], skipHeader=True)
    else:
        print """
        Generate INSERT statements for tab delimited text files.
        
        Syntax: oracle_insert_generate <file(s) mask> [header]
        
        <file(s) mask>  - tab delimited file(s) to be processed
        [header]        - whether header is present and should be skipped
                          alternatives recognised: 
                            'header=1', 'header', 'hasheader'
        """
